<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="teacher"
      status-icon
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="工号" prop="teacherNo">
        <el-input
          readonly
          v-model="teacher.teacherNo"
          placeholder="请输入工号"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="teacherName">
        <el-input
          v-model="teacher.teacherName"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="teacherPhone">
        <el-input
          v-model="teacher.teacherPhone"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="teacherEmail">
        <el-input
          v-model="teacher.teacherEmail"
          placeholder="请输入邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="薪水" prop="teacherSalary">
        <el-input
          v-model="teacher.teacherSalary"
          placeholder="请输入薪水"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="teacher.teacherSex" size="medium">
          <el-radio border :label="0">男</el-radio>
          <el-radio border :label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生日期" prop="teacherBirthday">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="teacher.teacherBirthday"
            style="width: 100%"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="经验" prop="teacherExperience">
        <el-select v-model="teacher.teacherExperience" placeholder="请选择经验">
          <el-option label="1-3年" :value="1"></el-option>
          <el-option label="3-5年" :value="2"></el-option>
          <el-option label="5-10年" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="学科" prop="subject">
        <el-select v-model="sizeForm.subject" placeholder="请选择学科">
          <el-option label="Java" value="0"></el-option>
          <el-option label="JavaScript" value="1"></el-option>
           <el-option label="HTML" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入学日期" prop="inSchoolDate">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="sizeForm.inSchoolDate"
            style="width: 100%"
          ></el-date-picker>
        </el-col>
      </el-form-item> -->
      <el-form-item label="老师形象">
        <el-upload
          class="upload-demo"
          accept=".jpg,.png"
          action=""
          show-file-list
          :before-upload="beforeUpload"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="现住地址" prop="teacherNowAddress">
        <el-input
          v-model="teacher.teacherNowAddress"
          placeholder="请输入现住地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="家乡地址" prop="teacherHomeAddress">
        <el-input
          v-model="teacher.teacherHomeAddress"
          placeholder="请输入家乡地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="技能">
        <tag
          :tags="teacher.teacherSkill"
          :add-tag="addTag"
          :delete-tag="deleteTag"
        ></tag>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { nanoid } from "nanoid";
import rules from "../commons/validate";

import { useAddTeacher } from "@/hooks/teacher";

export default {
  name: "TeacherAdd",
  data() {
    return {
      rules,
      teacher: {
        teacherNo: nanoid(),
        teacherName: "",
        teacherPhone: "",
        teacherEmail: "",
        teacherSalary: "",
        teacherSex: "",
        teacherBirthday: "",
        teacherExperience: "",
        teacherSkill: [],
        teacherNowAddress: "",
        teacherHomeAddress: "",
      },
      fileList: [],
    };
  },

  mounted() {},

  methods: {
    customTipText(maxTagNumber) {
      return "这是自定义的内容，加上" + maxTagNumber;
    },
    // 添加 tag
    addTag(tag, callback) {
      if (!tag) {
        callback(() => {
          this.$message({
            type: "error",
            message: `标签不能重复`,
          });
        });
      } else {
        this.teacher.teacherSkill.push(tag);
      }
    },
    // 删除 tag
    deleteTag(tagIndex) {
      this.teacher.teacherSkill.splice(tagIndex, 1);
    },
    beforeUpload(file) {
      this.teacher.teacherAvatar = file;
      this.fileList.push(file);
      return false;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let teacher = new FormData();
          for (let k in this.teacher) {
            if (k === "teacherBirthday" ) {
              teacher.append(k, new Date(this.teacher[k]).getTime());
            } else {
              teacher.append(k, this.teacher[k]);
            }
          }
          const resInfo = await useAddTeacher(teacher);
          if (resInfo.code === 201 && resInfo.message === "success") {
            this.$message.success("注册成功,请注意查收邮件信息进行登录..");
            this.teacher.studentNo = nanoid();
          } else {
            this.$message.success(resInfo.mesage);
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped></style>
